<?php
	include_once("../Business/survey_service.php");
	include_once("../Business/survey_question_service.php");
	include_once("../Business/survey_answer_service.php");
	include_once("../Business/survey_answer_user_service.php");
	
	include_once("../Business/Data/Entity/survey.php");
	include_once("../Business/Data/Entity/survey_question.php");
	include_once("../Business/Data/Entity/survey_answer.php");
	include_once("../Business/Data/Entity/survey_answer_user.php");
	
	include_once("uitilities.php");
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$ids = split(" ",post("ids"));
		foreach($ids as $id){
			survey_question_service::delete_survey_question($id);
		}	
	}
	
	$data = '';

	$id = get("id");
	$s = survey_service::get_survey($id);
	if($s)
	{
		$sqlist = survey_question_service::get_survey_question_list_by_survey_id($s->survey_id);
			
		for($i=0 ; $i<count($sqlist) ; $i++) {
			$sq = $sqlist[$i];
			$salist = survey_answer_service::get_survey_answer_list_by_survey_question_id($sq->survey_question_id);
			$totalNumber = survey_answer_user_service::get_number_by_survey_question_id($sq->survey_question_id);
			$data = $data.'
			<tr class="'.($i%2==0?"even":"odd").'">
				<td class="checkcol">
					<input type="checkbox" name="cb" onclick="updateIDs()">
					<input type="hidden" name="id" value="'.$sq->survey_question_id.'">
				</td>
				<td>
					<dl>
						<dt>Question:</dt>
						<dd><a name="c" href="javascript:void(0)">'.encodeS($sq->survey_question_content).'</a></dd>
						
					</dl>
					
				</td>
				<td style="text-align:center">
					'.$totalNumber.'
				</td>
			</tr>
			';
			for($j=0 ; $j<count($salist) ; $j++) {
				$sa = $salist[$j];
				$number = survey_answer_user_service::get_number_by_survey_answer_id($sa->survey_answer_id);
				$percent = ($number * 100 / ($totalNumber >0 ? $totalNumber : 1));
				
				
				$data = $data.'
				<tr class="'.($i%2==0?"even":"odd").'">
					<td class="checkcol">
					</td>
					<td>
						<dt>Answer:</dt>
						<dd><a name="c" href="javascript:void(0)">'.encodeS($sa->survey_answer_content).'</a></dd>
						
					</td>
					<td style="text-align:center">
						'.sprintf("%.2f", $percent).'% ('.$number.'/'.$totalNumber.')
					</td>
				</tr>
				';
			}
		}
	}


	$msgstatus = 'display:none';
	$msg = '';
	$main_content_title = '';
	$main_content = '
	<h3>Survey question list</h3>
<form method="post">
	<table class="survey">
        <tbody>
            <tr class="even">
                <td>
                    <dl>
                        <dt><a name="lName" href="javascript:void(0)">Name</a></dt>
                    </dl>
                </td>
                <td>
					<input type="text" name="iName" disabled="true"  value="'.($s ? encodeS($s->survey_name) : '').'" />
                </td>
            </tr>
        </tbody>
    </table>
<input type="hidden" name="ids" value="">
<p class="label-submit">
	<input value="Delete" type="submit" name="btnDelete" onclick="return confirmDelete(\'answer(s)\');">
</p>
<table class="contentdata"  style="width:100%">
	<thead>
		<tr>
			<th style="width:2%" class="checkcol"><input type="checkbox" name="cb" onclick="checkAll(this)"></th>
			<th style="width:80%">Content</th>
			<th style="text-align:center">User answer</th>
	</thead>
	<tbody>
		'.$data.'
	</tbody>
        <thead>
		<tr>
			<th style="width:2%" class="checkcol"><input type="checkbox" name="cb" onclick="checkAll(this)"></th>
			<th style="width:80%">Content</th>
			<th style="text-align:center">User answer</th>
	</thead>
</table>
<p class="label-submit">
	<input value="Delete" type="submit" name="btnDelete" onclick="return confirmDelete(\'answer(s)\');">
</p>
</form>
<script>
	function checkAll(cb) {
		var cbs = $("input[name=cb]")
		cbs.each(function(index) {
			$(this).attr("checked", cb.checked);
		});
	}
	function updateIDs() {
		var ids = "";
		$("input:checked[name=cb]").each(function(index) {
			ids += " " +$(this).next().val();
		});
		$("input[name=ids]").val(ids);
	}
        function init() {

                var data = htmlspecialchars_decode($("input[name=iName]").val());
                if(data.indexOf("<")>=0) {
                $("a[name=lName]").tooltip({ 
				width: 200,
				track: true,
				delay: 2, 
				showURL: false, 
				bodyHandler: function() { 
					return $(data );
				} 
			});
                }

		$("a[name=c]").each(function(index) {			
                var data = htmlspecialchars_decode($(this).html());

                if(data.indexOf("<")>=0) {
			$(this).tooltip({ 
				width: 200,
				track: true,
				delay: 2, 
				showURL: false, 
				bodyHandler: function() { 
                                      return $(data);
				} 
			});
                }
		});
                
	}
	init();
</script>
';
	
	include_once("masters/pages.php");
?>